package cn.bgs.controller;

import cn.bgs.pojo.Lian;
import cn.bgs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class UserController {
    @Autowired private UserService userService;


/*

    //返回多条数据以map返回
    @RequestMapping("/findMaps")
    @ResponseBody
    public Map<Integer,User> findMaps(){
        String name="马";
        return userService.findMaps(name);
    }
*/




 @RequestMapping("/QueryLian")

    public String QueryLian(String name,Model m){



       List<Lian> lian= userService.QueryLian(name);

        m.addAttribute("list",lian);

        return "success";
    }

  @RequestMapping("/QueryLianBirthday")
  public String QueryLianBirthday(String birthday,Model m){


    Map<Integer,Lian>  lian= userService.QueryLianBirthday(birthday);

      m.addAttribute("list",lian);

      return "success";
  }








    @RequestMapping("login")
    public  String login(String name, String password, HttpSession session, Model m){


        Lian user = userService.login(name, password);

        if (user != null) {

            //session.setAttribute("userName", user.getName());
            session.setAttribute("ww", user);
            return "redirect:/findAll";

        } else {

            m.addAttribute("error", "用户名或密码错误");

            return "login";

        }
    }


    @RequestMapping("/findAll")
    public  String findAll(Model m){

      List<Lian> list= userService.findAll();

        m.addAttribute("list",list);

        return "success";

    }

    @RequestMapping("/findById/{id}")
    public  String findById(@PathVariable Integer id,Model m){

        Lian lian=userService.findById(id);

        m.addAttribute("list",lian);

        return "modify";

    }





    @RequestMapping("/update")
    public String update(Lian lian, MultipartFile pictureFile) throws IOException {

        //先要判断上传文件的有效性
        //要判断上传文件的字节数
        long size = pictureFile.getSize();

        if(size>0){
            //获取文件的名称
            String oldName = pictureFile.getOriginalFilename();

            //解决图片名称重复问题
            //我们使用UUID来解决
            String uuid = UUID.randomUUID().toString();
            //去掉小横杆
            String replace = uuid.replace("-", "");
            //全部变成大写
            String name = replace.toUpperCase();
            //获取原文件的后缀
            //先找到原文件中.最后出现的位置
            int lastIndex = oldName.lastIndexOf(".");
            //从最后一个.开始截取后面全要了
            String suffix = oldName.substring(lastIndex);
            //组成永不重复的新的文件的名称
            String newName=name+suffix;


            //上传文件,把文件存入文件系统
            pictureFile.transferTo(new File("E:\\photo\\"+newName));

            //把文件名称存入数据库中
            lian.setTupian(newName);

            //完成修改

            userService.update(lian);
        }else{
            //完成修改

            userService.update(lian);
        }

        //重定向
        return "redirect:/findAll";


    }



}
